package com.white.algebra.service.impl;

import com.white.algebra.dao.DiscussDao;
import com.white.algebra.dao.impl.DiscussDaoImpl;
import com.white.algebra.domain.Discuss;
import com.white.algebra.domain.Discussion;
import com.white.algebra.listener.RedisWork;
import com.white.algebra.service.DiscussService;
import com.white.algebra.service.UserService;
import com.white.algebra.utils.SensitiveFilterService;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class DiscussServiceImpl implements DiscussService {
    private DiscussDao discussDao=new DiscussDaoImpl();
    @Override
    public List<Discussion> findAll(int startRecord, int size,String name) throws Exception {
        UserService userService=new UserServiceImpl();
        List<Discussion> discussions= discussDao.findAll( startRecord,  size, name);
        for (Discussion discussion:discussions
             ) {
            discussion.setAuthorInfo(userService.findById(discussion.getAuthor()));
        }
        return discussions;
    }

    @Override
    public void insertDiscussion(Discussion discussion) throws SQLException {
        SensitiveFilterService filter = SensitiveFilterService.getInstance();
        String text = filter.replaceSensitiveWord(discussion.getText(), 2, "*");
        discussion.setText(text);
        discussDao.insertDiscussion(discussion);
    }

    @Override
    public Discussion findDiscussionById(String id, int startRecord, int size) throws Exception {
        UserService userService=new UserServiceImpl();
        Discussion discussion=discussDao.findDiscussionById(id);
        discussion.setAuthorInfo(userService.findById(discussion.getAuthor()));
        List<Discuss> discussList=discussDao.findDiscussByDiscussionId(id,startRecord,size);
        Map<String, String> map= RedisWork.findAll();
        for(Discuss d:discussList){
            if(map.containsKey(d.getId())){
                d.setThumbsUp(Integer.parseInt(map.get(d.getId())));
            }
            d.setAuthorInfo(userService.findById(d.getAuthor()));
            if(d.getDiscussId()!=null){
                d.setReplyDiscuss(discussDao.findDiscussById(d.getDiscussId()));
                String replyAuthorId=d.getReplyDiscuss().getAuthor();
                d.getReplyDiscuss().setAuthorInfo(userService.findById(replyAuthorId));
            }
        }
        discussion.setDiscuss(discussList);
        return discussion;
    }

    @Override
    public int getDiscussionNum(String name) throws SQLException {
        return discussDao.getDiscussionNum(name);
    }

    @Override
    public int getDiscussNum(String id) throws SQLException {
        return discussDao.getDiscussNum(id);
    }

    @Override
    public void thumbUp(String id, String fun) {
        //t 点赞   f 去赞
        discussDao.thumbsUp( id,  fun);
    }

    @Override
    public void insertDiscuss(Discuss discuss) throws SQLException {
        SensitiveFilterService filter = SensitiveFilterService.getInstance();
        String text = filter.replaceSensitiveWord(discuss.getText(), 2, "*");
        discuss.setText(text);
        discussDao.insertDiscuss(discuss);
        discussDao.viewDiscussion(discuss.getDiscussionId());
    }

}
